package com.platform.service.impl;

import com.github.pagehelper.PageInfo;
import com.platform.annotation.SysLog;
import com.platform.base.BaseService;
import com.platform.common.Const;
import com.platform.common.ServerResponse;
import com.platform.pojo.Psad0001;
import com.platform.pojo.User;
import com.platform.service.IPsad0001Service;
import com.platform.util.DateTimeUtil;
import com.platform.util.MapperUtil;
import com.platform.util.NumberUtil;
import com.platform.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 专家信息服务层
 */
@Service("iPsad0001Service")
public class Psad0001ServiceImpl extends BaseService implements IPsad0001Service {

    /**
     * 根据条件查询专家信息
     * @param paramMap
     * @return
     */
    @SysLog("查询专家信息")
    public ServerResponse<PageInfo> selectByParamMap(Map paramMap){
        return MapperUtil.pageInfo(paramMap,psad0001Mapper);
    }

    /**
     * 查询专家用户详细信息
     * @param userName
     * @return
     */
    @SysLog("查询专家用户详细信息")
    public ServerResponse<Map> selectByUserName(String userName){
        Map resultMap = psad0001Mapper.selectByUserName(userName);
        //将性别转成string 适应前台
        resultMap.put("sex",StringUtil.valueOfString(resultMap.get("sex")));
        resultMap.put("is_on_job",StringUtil.valueOfString(resultMap.get("is_on_job")));
        resultMap.put("is_emergency",StringUtil.valueOfString(resultMap.get("is_emergency")));
        resultMap.put("high_educatign_background",StringUtil.valueOfString(resultMap.get("high_educatign_background")));
        if(null != resultMap){
            return ServerResponse.createBySuccess(Const.Msg.SELECTSUCCESS,resultMap);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.SELECTERROR);
    }

    /**
     * 更新专家信息
     * @param paramMap
     * @return
     */
    public ServerResponse<String> update(Map paramMap, HttpServletRequest request){
        //查询版本号是否一致
        int oldVersion = NumberUtil.safeToNumber(paramMap.get("record_version"));
        int id = NumberUtil.safeToNumber(paramMap.get("id"));
        if(id == 0){
            return ServerResponse.createByErrorMessage("数据错误");
        }
        if(!MapperUtil.selectVersionIsChangeById(id,oldVersion,psad0001Mapper)){
            return ServerResponse.createByErrorMessage("数据被更新");
        }
        //获取操作人
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == o){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        User user = (User)o;
        if(null == user){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        Psad0001 psad0001 = new Psad0001();
        psad0001.setId(id);//id
        psad0001.setUpdateTime(new Date());//更新时间
        psad0001.setUpdateUser(user.getUsername());//操作人
        psad0001.setName(StringUtil.valueOfString(paramMap.get("name")));//姓名
        psad0001.setSex(NumberUtil.safeToNumber(paramMap.get("sex")));//性别
        psad0001.setIdCardType(StringUtil.valueOfString(paramMap.get("id_card_type")));//证件类型
        psad0001.setIdCardCode(StringUtil.valueOfString(paramMap.get("id_card_code")));//身份证号码
        psad0001.setBirthDate(StringUtil.valueOfString(paramMap.get("birth_date")));//出生日期
        //查询省编码
        String province = enumMapper.selectValuesByCatalogAndTypeAndNames(
                StringUtil.valueOfString(paramMap.get("catalog")),
                StringUtil.valueOfString(paramMap.get("type")),
                StringUtil.valueOfString(paramMap.get("provincename")));
        psad0001.setProvince(province);//省编码
        //查询市编码
        String city = enumMapper.selectValuesByCatalogAndTypeAndNames(
                StringUtil.valueOfString(paramMap.get("catalog")),
                StringUtil.valueOfString(paramMap.get("type")),
                StringUtil.valueOfString(paramMap.get("cityname")));
        psad0001.setCity(city);//市编码
        psad0001.setGraduateSchoole(StringUtil.valueOfString(paramMap.get("graduate_schoole")));//毕业院校
        psad0001.setHighEducatignBackground(StringUtil.valueOfString(paramMap.get("high_educatign_background")));//最高学历
        psad0001.setEducationCertificateNo(StringUtil.valueOfString(paramMap.get("education_certificate_no")));//学历编号
        psad0001.setLearnSpeciality(StringUtil.valueOfString(paramMap.get("learn_speciality")));//所学专业
        String graduate_date = StringUtil.valueOfString(paramMap.get("graduate_date"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(StringUtil.isNotEmpty(graduate_date)) {
            try {
                psad0001.setGraduateDate(sdf.parse(graduate_date));//毕业时间
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        psad0001.setLanguageLevel(StringUtil.valueOfString(paramMap.get("language_level")));//外语程度
        psad0001.setMoble(StringUtil.valueOfString(paramMap.get("moble")));//手机号
        psad0001.setTelphone(StringUtil.valueOfString(paramMap.get("telphone")));//联系电话
        psad0001.setFax(StringUtil.valueOfString(paramMap.get("fax")));//传真
        psad0001.setEmail(StringUtil.valueOfString(paramMap.get("email")));//邮箱
        psad0001.setAddress(StringUtil.valueOfString(paramMap.get("address")));//通讯地址
        psad0001.setZip(StringUtil.valueOfString(paramMap.get("zip")));//邮编
        psad0001.setIsOnJob(NumberUtil.safeToNumber(paramMap.get("is_on_job")));//是否在职
        psad0001.setWorkCompany(StringUtil.valueOfString(paramMap.get("work_company")));//在职单位
        psad0001.setWordResume(StringUtil.valueOfString(paramMap.get("word_resume")));//工作简历
        psad0001.setWorkTitle(StringUtil.valueOfString(paramMap.get("work_title")));//技术职称
        psad0001.setLevelInfo(StringUtil.valueOfString(paramMap.get("level_info")));//职业资格等级
        psad0001.setProQualificationCode(StringUtil.valueOfString(paramMap.get("pro_qualification_code")));//职业资格名称及序列号
        psad0001.setIsEmergency(StringUtil.valueOfString(paramMap.get("is_emergency")));//是否应急专家
        psad0001.setIsSecurity(StringUtil.valueOfString(paramMap.get("is_security")));//是否涉密专家
        psad0001.setProQualificationNo(StringUtil.valueOfString(paramMap.get("pro_qualification_no")));//职称证书编号
        psad0001.setRegisterNo(StringUtil.valueOfString(paramMap.get("register_no")));//注册登记证书编号
        psad0001.setEmploymentYear(StringUtil.valueOfString(paramMap.get("employment_year")));//从业年限
        psad0001.setWorkDepartment(StringUtil.valueOfString(paramMap.get("work_department")));//工作部门/科室
        if(psad0001Mapper.updateByPrimaryKeySelective(psad0001) > 0){
            return ServerResponse.createBySuccessMessage(Const.Msg.UPDATESUCCESS);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.UPDATEERROR);
    }


    /**
     * 更新专家状态（0 保存、1生效、2无效、3 待审核、4驳回）
     * @param paramMap
     * @return
     */
    public ServerResponse<String> updatePsadStatus(Map paramMap,HttpServletRequest request){
        int id = NumberUtil.safeToNumber(paramMap.get("id"));
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == o){
           return ServerResponse.createByErrorMessage("操作人员错误");
        }
        User u = (User)o;
        paramMap.put("userName",u.getUsername());//操作人
        if(id <= 0){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        if(psad0001Mapper.updatePsadStatus(paramMap) > 0){
            return ServerResponse.createBySuccessMessage(Const.Msg.UPDATESUCCESS);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.UPDATEERROR);

    }

}
